<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

use think\Config;
use think\Session;
use fast\Http;

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;


class Awstest extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

    }

    //http://fastaws.test/api/awstest/test_s3
    public function test_s3()
    {
        //Create an Amazon S3 client
        $s3 = new S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);


        try {
            //获取bucket列表
//            $result = $s3->listBuckets();
//            foreach($result['Buckets'] as $b) {
//                var_dump($b);
//            }

            //上传文件
            $s3->putObject([
                'Bucket' => 'my-bucket',
                'Key'    => 'my-object',
                'Body'   => fopen('robots.txt', 'r'),
                'ACL'    => 'public-read',
            ]);

            //上传obj
            $bucket = 'my-bucket';
            $result = $s3->putObject(array(
                'Bucket' => $bucket,
                'Key' => '201711/404.html',
                'SourceFile' => './404.html',
                'Metadata' => array(
                    'Foo' => 'abc',
                    'Bar' => '123'
                )
            ));

            //copy
            $result = $s3->copyObject(array(
                'Bucket' => $bucket,
                'CopySource' => urlencode($bucket . '/' . '201711/404.html'),
                'Key' => '201711/606.html'
            ));

            //获取obj列表
            $iterator = $s3->getIterator('ListObjects', array('Bucket' => $bucket));
            foreach ($iterator as $object) {
                //if(strpos($object['Key'], 'log/')!== false){
                /* $client->deleteObject([
                    'Bucket' => $bucket,
                    'Key'    => $object['Key']
                ]); */
                echo $object['Key'] . "\n";
                //}
            }


        } catch (S3Exception $e) {
            echo "There was an error uploading the file.\n";
        }

        echo "test!";
    }

    //http://fastaws.test/api/awstest/phpinfo
    public function phpinfo()
    {
        
        phpinfo();
    }




}





